package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Ad;
import com.czz.manage.query.AdQuery;
import com.czz.manage.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: AdAdminController
 * @Author Czz
 * @Date 2020/2/5
 * @Time 17:34
 **/
@RestController
@RequestMapping(value = "/admin/ads")
public class AdAdminController {

    @Autowired
    AdService adService;

    /**
    * @Description 分页查询所有轮播图
    * @Param [page, limit, adQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/5
    * @Time 18:28
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getAdPageList(@PathVariable Long page, @PathVariable Long limit, AdQuery adQuery) {
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Ad> pageParam = new Page<>(page,limit);
        adService.getAdPageList(pageParam, adQuery);
        List<Ad> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 根据id删除轮播图
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/5
    * @Time 18:31
    **/
    @DeleteMapping(value = "{id}")
    public R deleteAdById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Boolean flag = adService.deleteAdById(id);
        if (flag) return R.ok();
        else return R.setResult(ResultCodeEnum.INPUT_ERROR).message("删除失败！至少需存在一张轮播图！");
    }

    /**
    * @Description 根据id启用/禁用轮播图
    * @Param [id, status]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/5
    * @Time 18:33
    **/
    @PutMapping(value = "/{id}/{status}")
    public R updateAdStatusById(@PathVariable String id,@PathVariable Integer status){
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        adService.updateAdStatusById(id,status);
        return R.ok();
    }

    /**
    * @Description 根据id修改轮播图
    * @Param [id, ad]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/5
    * @Time 19:16
    **/
    @PutMapping(value = "{id}")
    public R updateAdById(@PathVariable String id,Ad ad){
        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(ad)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        ad.setId(id);
        adService.updateAdById(ad);
        return R.ok();
    }

    /**
    * @Description 根据id查询轮播图
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/5
    * @Time 19:19
    **/
    @GetMapping(value = "{id}")
    public R getAdById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Ad ad = adService.getById(id);
        return R.ok().data("item",ad);
    }

    /**
    * @Description 新增轮播图
    * @Param [ad]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/13
    * @Time 10:31
    **/
    @PostMapping(value = "")
    public R saveAd(Ad ad){
        if (ObjectUtils.isEmpty(ad)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        adService.save(ad);
        return R.ok();
    }
}